package com.jxs.android.cmcc;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.cmcc.data.KipInfo;
import com.jxs.android.cmcc.db.KipConfigHolder;

public class KipSelectList extends KipSelectAbstract {

	private ListView applistView = null;
	private Context context;

	private List<KipInfo> kipInfos = new ArrayList<KipInfo>();

	private KipLstAdpter kipLstAdpter;
	private TextView empty_tv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_select_kip_lst);
		empty_tv = (TextView) findViewById(R.id.empty_tv);
		context = this;
		applistView = (ListView) findViewById(R.id.main_kip_list);
		kipLstAdpter = new KipLstAdpter(context);
		applistView.setAdapter(kipLstAdpter);

	}

	@Override
	public String getMethod() {
		return "getKpiTypeList";
	}

	@Override
	protected void handlerResult(List<KipInfo> kipInfos) {
		this.kipInfos.clear();
		this.kipInfos.addAll(kipInfos);
		kipLstAdpter.notifyDataSetChanged();
		if (this.kipInfos.size() == 0) {
			empty_tv.setVisibility(View.VISIBLE);
		} else {
			empty_tv.setVisibility(View.GONE);
		}
	}

	@Override
	protected String getTopBarTitle() {
		return "添加指标";
	}

	private class KipLstAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;

		public KipLstAdpter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return kipInfos.size();
		}

		@Override
		public KipInfo getItem(int position) {
			return kipInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.kip_list_item, null);

				viewHolder.kip_item_name = (TextView) convertView.findViewById(R.id.kip_item_name);
				viewHolder.kip_item_main = (TextView) convertView.findViewById(R.id.kip_item_main);
				viewHolder.kip_item_sub = (TextView) convertView.findViewById(R.id.kip_item_sub);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			final KipInfo info = kipInfos.get(position);
			viewHolder.kip_item_name.setText(info.getKpi_name());
			viewHolder.kip_item_main.setText("主指标:" + info.getKpi_name_1());
			viewHolder.kip_item_sub.setText("附属指标:" + info.getKpi_name_2() + "," + info.getKpi_name_3());

			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					KipAddOrDel("addKpi", position, info);
				}
			});

			return convertView;
		}

		class ViewHolder {
			TextView kip_item_name;
			TextView kip_item_main;
			TextView kip_item_sub;
		}
	}

	@Override
	protected void nextStep(int position) {
		kipInfos.remove(position);
		kipLstAdpter.notifyDataSetChanged();
		if (this.kipInfos.size() == 0) {
			empty_tv.setVisibility(View.VISIBLE);
		} else {
			empty_tv.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		refresh(KipConfigHolder.Scope.UN_SELECTED);
	}

}